import $ from '@/common'
// 标准数据类型
import Base from 'field/control/base'
import Code from 'field/control/code'
import Memo from 'field/control/memo'

import State from 'field/control/state'
import State_all from 'field/control/stateAll'

import Password from 'field/control/password'
import Bool from 'field/control/bool'
import Ref from 'field/control/ref'
import Integer from 'field/control/integer'
import Money from 'field/control/money'

import Timestamp from 'field/control/timestamp'
import date from 'field/control/date'

// 范围类型
import Timestamp_range from 'field/control/timestampRange'
import Date_range from 'field/control/dateRange'
import Number_range from 'field/control/numberRange'

// 转化类型
import Check from 'field/control/check'
import Select from 'field/control/select'
import Inline_input from 'field/control/inlineInput'
import Input_select from 'field/control/inputSelect'
import Code_select from 'field/control/codeSelect'

// 特殊类型
import Exp_condition from 'field/control/expCondition'
const baseType = ['code', 'timestamp', 'date', 'money', 'seriescode', 'stat', 'clob', 'integer', 'ref']
const Controls = {
  Base,
  Code,
  Memo,
  State,
  Integer,
  Stat: State,
  State_all,
  Select,
  Password,
  Bool,
  Check,
  Ref,
  Money,
  Date: date,
  Timestamp,
  Timestamp_range,
  Date_range,
  Number_range,
  Inline_input,
  Input_select,
  Code_select,
Exp_condition}

// 行内编辑对应
const inlineControls = {
  memo: 'inline_input',
  bool: 'check',
  state: 'select',
  stat: 'select',
  code: 'inline_input'
}
const searchControls = {
  memo: 'code',
  state: 'state_all',
  stat: 'state_all'
}
const searchRangeControls = {
  integer: 'number_range',
  number: 'number_range',
  money: 'number_range',
  date: 'date_range',
  timestamp: 'timestamp_range'
}

const fieldControls = {}
for (let i in Controls) {
  fieldControls['Field' + i] = Controls[i]
}

function replaceStr (str) {
  if (!str) return str
  str = str.toLowerCase()
  var reg = /\b(\w)|\s(\w)/g
  return str.replace(reg, function (m) {
    return m.toUpperCase()
  })
}

export default {
  fieldType(field) {
    var code = 'Field' + replaceStr(field.type)
    if (field.type === 'code' && field.rangeset) {
      if (field.rangeset.indexOf && (field.rangeset.indexOf('@') > -1)) {
        code = 'FieldState'
      } else {
        code = 'FieldCode_select'
      }
    }
    if (fieldControls[code]) {
      return code
    } else {
      return 'FieldBase'
    }
  },
  inlineFieldType(field) {
    var type = field.type
    // 进行类型转化
    if (inlineControls[type]) {
      type = inlineControls[type]
    }
    if ($.isArray(field.rangeset)) {
      type = 'select'
    }
    var code = 'Field' + replaceStr(type)
    if (fieldControls[code]) {
      return code
    } else {
      return 'FieldBase'
    }
  },
  searchFieldType(field, setting = {}) {
    var type = field.type
    if (searchControls[type]) {
      type = searchControls[type]
    }
    if (type === 'state_all' && setting.stateAll === false) {
      type = 'state'
    }
    if (field.param && field.param.range) {
      type = searchRangeControls[type] || type
    }
    var code = 'Field' + replaceStr(type)
    if (type === 'code' && field.rangeset) {
      if (field.rangeset.indexOf && (field.rangeset.indexOf('@') > -1)) {
        code = 'FieldState'
      }
    }
    if (fieldControls[code]) {
      return code
    } else {
      return 'FieldBase'
    }
  },
  control: fieldControls,
  baseType: baseType
}
